import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:share_fit/app/controllers/base/base_controller.dart';
import 'package:share_fit/app/controllers/home/home_const.dart';
import 'package:share_fit/app/models/home/edit_widget_item.dart';
import 'package:share_fit/app/ute/uteped_const.dart';
import 'state.dart';

class EditWidgetLogic extends BaseController {
  final EditWidgetState state = EditWidgetState();

  void removeFromHasList(EditWidgetItem toRemove) {
    toRemove.action = 'add';
    state.hasList.remove(toRemove);
    state.sourceList.add(toRemove);
    update();
  }

  void addToHasList(EditWidgetItem toAdd) {
    toAdd.action = 'remove';
    state.hasList.add(toAdd);
    state.sourceList.remove(toAdd);
    update();
  }

  goBack(BuildContext context) {
    Navigator.pop(context, state.hasList);
  }

  onDragAccept(data, index) {
    final temp = state.hasList[data];
    state.hasList.remove(temp);
    state.hasList.insert(index, temp);
    update();
  }

  @override
  String getRemotePath() {
    // TODO: implement getRemotePath
    return UTEPedConst.pathHome;
  }

  @override
  void initState() {
    if (Get.arguments != null) {
      state.hasList = Get.arguments;
      state.sourceList.removeWhere((l) => state.hasList.indexWhere((element) => l.type.getTitle == element.type.getTitle) > -1);

      for (EditWidgetItem item in state.hasList) {
        item.width = 1;
        item.height = 1;
      }
      for (EditWidgetItem item in state.sourceList) {
        item.width = 1;
        item.height = 1;
      }
    }
  }

  @override
  void onRemoteEvent(event, method) {
    // TODO: implement onRemoteEvent
  }

  @override
  void onClose() {
    super.onClose();
    List maps = state.hasList.map((e) => e.toMap()).toList();
    save(HomeConst.hasWidgetList, maps, global: true);
  }
}
